package chapter1.fundamentals.exercise;

import edu.princeton.cs.algs4.StdDraw;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdRandom;

import java.awt.*;

/**
 * @author quanlinlin on 2018/11/3 11:44.
 * @version 1.0
 */
public class E_31 {

    public static void main(String[] args) {
        int N = StdIn.readInt();
        double p = StdIn.readDouble();
        assert p > 0 && p < 1;

        drawPoint(N, p);
    }

    private static void drawPoint(int n, double p) {
        StdDraw.setCanvasSize(512, 512);
        double radius = 5;

        StdDraw.setXscale(-(radius + 1), radius + 1);
        StdDraw.setYscale(-(radius + 1), radius + 1);
        StdDraw.setPenColor(Color.BLUE);
        StdDraw.circle(0, 0, radius);    //画圆完成

        double angle = Math.PI * 2 / n;
        StdDraw.setPenRadius(0.01);
        double[][] points = new double[n][2];
        for (int i = 0; i < n; i++) {
            double pointX = radius * Math.cos(i * angle + Math.PI / 2);
            double pointY = radius * Math.sin(i * angle + Math.PI / 2);
            points[i][0] = pointX;
            points[i][1] = pointY;
            StdDraw.point(pointX, pointY);
        }

        StdDraw.setPenColor(StdDraw.GRAY);
        StdDraw.setPenRadius();
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                if (StdRandom.uniform() < p) {
                    randomColor();
                    StdDraw.line(points[i][0], points[i][1], points[j][0], points[j][1]);
                }
            }
        }
    }

    private static void randomColor() {
        StdDraw.setPenColor(Color.getHSBColor(((float) StdRandom.uniform()), ((float) StdRandom.uniform()), (float) StdRandom.gaussian(30.0, 5)));
    }
}
